import { getForm, getColumns, getFilters, getTableData, createFormData, updateFormData, deleteFormData, deletesFormData, getTreeTableData, addFilter, deleteFilter, updateFilter, updateColumn, getFormDetail } from '@/api/form'

export default {
    state: {
        forms: {},// 表单集合
        tableColumns: {},// 表格显示列
        tableFilters: {}// 表单自定义查询条件
    },
    mutations: {
        setForms(state, form) {
            state.forms[form.id] = form;
        },
        setTableColumns(state, data) {
            state.tableColumns[data.form_id] = data.data;
        },
        setTableFilters(state, data) {
            state.tableFilters[data.form_id] = data.data;
        }
    },
    actions: {
        // 获取表单信息
        getForm({ state, commit }, { form_id }) {
            return new Promise((resolve, reject) => {
                try {
                    if (state.forms[form_id]) {
                        resolve(state.forms[form_id])
                        return;
                    }
                    getForm(form_id).then(res => {
                        const data = res.data
                        commit('setForms', data.data)
                        resolve(data.data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 获取表格显示列
        getColumns({ state, commit }, { form_id, is_refresh }) {
            return new Promise((resolve, reject) => {
                try {
                    if (state.tableColumns[form_id] && !is_refresh) {
                        resolve(state.tableColumns[form_id])
                        return;
                    }

                    getColumns(form_id).then(res => {
                        const data = res.data
                        commit('setTableColumns', { form_id, data: data.data })
                        resolve(data.data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 更新自定义显示列
        updateColumns({ state, commit }, { form_id, list }) {
            return new Promise((resolve, reject) => {
                try {
                    updateColumn(form_id, list.filter(f => f.type !== 'selection').map(f => { f.is_system = 0; f.sortable = 1; return f })).then(res => {
                        const data = res.data
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 获取自定义查询信息
        getFilters({ state, commit }, { form_id, is_refresh }) {
            return new Promise((resolve, reject) => {
                try {
                    if (state.tableFilters[form_id] && !is_refresh) {
                        resolve(state.tableFilters[form_id])
                        return;
                    }
                    getFilters(form_id).then(res => {
                        const data = res.data
                        commit('setTableFilters', { form_id, data: data.data })
                        resolve(data.data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 添加自定义筛选
        addFilters({ state, commit }, { form_id, filter }) {
            return new Promise((resolve, reject) => {
                try {
                    addFilter(form_id, filter).then(res => {
                        const data = res.data;
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 删除自定义筛选
        deleteFilters({ state, commit }, { filter_id }) {
            return new Promise((resolve, reject) => {
                try {
                    deleteFilter(filter_id).then(res => {
                        const data = res.data;
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 更新自定义筛选
        updateFilters({ state, commit }, { filter_id, filter }) {
            return new Promise((resolve, reject) => {
                try {
                    updateFilter(filter_id, filter).then(res => {
                        const data = res.data;
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 获取表格数据
        getTableData({ state, commit }, filter) {
            return new Promise((resolve, reject) => {
                try {
                    getTableData(filter).then(res => {
                        const data = res.data.data
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 获取树形表格数据
        getTreeTableData({ state, commit }, { form_id }) {
            return new Promise((resolve, reject) => {
                try {
                    getTreeTableData(form_id).then(res => {
                        const data = res.data
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 创建表单
        createFormData({ state, commit }, { form_id, data }) {
            return new Promise((resolve, reject) => {
                try {
                    createFormData(form_id, data).then(res => {
                        resolve(res.data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 更新表单
        updateFormData({ state, commit }, { form_id, data }) {
            return new Promise((resolve, reject) => {
                try {
                    updateFormData(form_id, data).then(res => {
                        const data = res.data
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        deletesFormData({ state, commit }, { form_id, ids }) {
            return new Promise((resolve, reject) => {
                try {
                    deletesFormData(form_id, ids).then(res => {
                        resolve(res)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 删除表单
        deleteFormData({ state, commit }, { form_id, id }) {
            return new Promise((resolve, reject) => {
                try {
                    deleteFormData(form_id, id).then(res => {
                        resolve(res)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 获取表单详情
        getFormDetail({ state, commit }, { form_id, id }) {
            return new Promise((resolve, reject) => {
                try {
                    getFormDetail(form_id, id).then(res => {
                        resolve(res)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        }

    }
}